//
//  Text2ImageViewController.m
//  ImageWorkshop
//
//  Created by Runli Song on 2023/11/24.
//  Copyright © 2023 iCocos. All rights reserved.
//

#import "Text2ImageViewController.h"
#import "LFPhotoEditingController.h"


@interface Text2ImageViewController () <UIImagePickerControllerDelegate,UINavigationControllerDelegate,LFPhotoEditingControllerDelegate>

@property (nonatomic, strong) UIImageView *mainImage;

@property (nonatomic, strong) UILabel *tipsLabel;

@property (nonatomic, strong) UIButton *entryBtn;

@property (nonatomic, strong) UIButton *doneBtn;

@end

@implementation Text2ImageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.title = @"画布";
    [self setLeftItemWithIcon:[UIImage imageNamed:@"back"] title:nil selector:@selector(onClickBack)];
  
    [self.view addSubview:self.doneBtn];
    [self.doneBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@(20));
        make.bottom.right.equalTo(@(-20));
        make.height.equalTo(@(50));
    }];
    
    [self.view addSubview:self.entryBtn];
    [self.entryBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.height.equalTo(self.doneBtn);
        make.bottom.equalTo(_doneBtn.mas_top).offset(-20);
    }];
    
    [self.view addSubview:self.mainImage];
    [self.mainImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.equalTo(@(20));
        make.right.equalTo(@(-20));
        make.bottom.equalTo(self.entryBtn.mas_top).offset(-20);
    }];
    
    [self.mainImage addSubview:self.tipsLabel];
    [self.tipsLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(@(0));
    }];
}

#pragma mark - actions

- (void)onClickBack {
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)onClickSelect{
    UIImagePickerController * picker = [[UIImagePickerController alloc]init];
    picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    picker.delegate = self;
    [self presentViewController:picker animated:YES completion:nil];
}

- (void)onClickEntry{
    if (!_mainImage.image) {
        [SVProgressHUD showErrorWithStatus:@"请先选择图片"];
        return;
    }
    LFPhotoEditingController *lfPhotoEditVC = [[LFPhotoEditingController alloc] init];
    lfPhotoEditVC.oKButtonTitleColorNormal = MainThemeColor;
    lfPhotoEditVC.isHiddenStatusBar = NO;
    lfPhotoEditVC.delegate = self;
    lfPhotoEditVC.editImage = self.mainImage.image;
//    [self presentViewController:lfPhotoEditVC animated:YES completion:nil];
    [self.navigationController setNavigationBarHidden:YES]; //隐藏导航栏（方式因项目自身适配）
    [self.navigationController pushViewController:lfPhotoEditVC animated:YES];
}

- (void)onClickDone{
    [UIImage saveImageToPhotoAlbum:self.mainImage.image];
}

#pragma mark - delegate

- (void)lf_PhotoEditingController:(LFPhotoEditingController *)photoEditingVC didCancelPhotoEdit:(LFPhotoEdit *)photoEdit{
    [self.navigationController setNavigationBarHidden:NO];
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)lf_PhotoEditingController:(LFPhotoEditingController *)photoEditingVC didFinishPhotoEdit:(LFPhotoEdit *)photoEdit{
    [self.navigationController setNavigationBarHidden:NO];
    [self.navigationController popViewControllerAnimated:YES];
    [self.mainImage setImage:photoEdit.editPosterImage];
}

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info
{
    UIImage * image = info[@"UIImagePickerControllerOriginalImage"];
    _mainImage.image = image;
    _tipsLabel.hidden = YES;
}

#pragma mark - setter

- (void)setImage:(UIImage *)image {
    _image = image;
    self.mainImage.image = _image;
}

#pragma mark - getter

- (UIImageView *)mainImage{
    if(!_mainImage){
        _mainImage = [[UIImageView alloc] init];
        _mainImage.backgroundColor = UIColorFromHex(0xf0f0f0);
        _mainImage.userInteractionEnabled = YES;
        _mainImage.contentMode = UIViewContentModeScaleAspectFit;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onClickSelect)];
        [_mainImage addGestureRecognizer:tap];
    }
    return _mainImage;
}

- (UILabel *)tipsLabel{
    if(!_tipsLabel){
        _tipsLabel = [[UILabel alloc] init];
        _tipsLabel.text = @"点击选择图片";
        _tipsLabel.textColor = MainThemeColor;
        _tipsLabel.userInteractionEnabled = YES;
    }
    return _tipsLabel;
}

- (UIButton *)entryBtn{
    if(!_entryBtn){
        _entryBtn = [UIButton buttonWithType:UIButtonTypeSystem];
        [_entryBtn setTitle:@"进入画布" forState:UIControlStateNormal];
        [_entryBtn setTitleColor:UIColor.whiteColor forState:UIControlStateNormal];
        _entryBtn.backgroundColor = MainThemeColor;
        _entryBtn.layer.cornerRadius = 10;
        _entryBtn.layer.masksToBounds = YES;
        [_entryBtn addTarget:self action:@selector(onClickEntry) forControlEvents:UIControlEventTouchUpInside];
    }
    return _entryBtn;
}

- (UIButton *)doneBtn{
    if(!_doneBtn){
        _doneBtn = [UIButton buttonWithType:UIButtonTypeSystem];
        [_doneBtn setTitle:@"保存到相册" forState:UIControlStateNormal];
        [_doneBtn setTitleColor:UIColor.whiteColor forState:UIControlStateNormal];
        _doneBtn.backgroundColor = MainThemeColor;
        _doneBtn.layer.cornerRadius = 10;
        _doneBtn.layer.masksToBounds = YES;
        [_doneBtn addTarget:self action:@selector(onClickDone) forControlEvents:UIControlEventTouchUpInside];
    }
    return _doneBtn;
}


@end
